//
//  HWTradecollectionShouCangViewController.m
//  HWanGame
//
//  Created by 陈道斌 on 2019/11/21.
//  Copyright © 2019 Chendaobin. All rights reserved.
//

#import "HWTradecollectionShouCangViewController.h"
#import "HWJiaoYiShouCangTableViewCell.h" // 我的游戏
#import "HWTrdingcollectionModel.h"
#import "HWAccountDetailsViewController.h"

@interface HWTradecollectionShouCangViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(strong,nonatomic)UITableView *mainaTableVie;
@property(strong,nonatomic)NSMutableArray *dataMuArr;
@property(strong,nonatomic) NSString *p;
@property(strong,nonatomic) NSString *max_p;


@end

@implementation HWTradecollectionShouCangViewController


-(UITableView *)mainaTableVie {

    if  (_mainaTableVie == nil) {

        _mainaTableVie = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenW, kScreenH) style:UITableViewStylePlain];
        _mainaTableVie.delegate = self;
        _mainaTableVie.dataSource = self;
        _mainaTableVie.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _mainaTableVie.backgroundColor = [UIColor whiteColor];
       // _mainaTableVie.backgroundColor = RGB(241, 242, 244);
        
    }
    return   _mainaTableVie ;

}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self.mainaTableVie.mj_header beginRefreshing];
    
    
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self addControlsView];
}


// 添加控件
-(void)addControlsView {
    
    
    self.view.backgroundColor = [UIColor orangeColor];
    self.navigationItem.title = @"我的收藏";
    
    [self.view addSubview:self.mainaTableVie];
    
    [self addrefreshControl];
    
    
}



#pragma  mark ---- 添加刷新

- (void)addrefreshControl {
    
    self.mainaTableVie.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(underRefresh)];
    
  // [self.mainaTableVie.mj_header beginRefreshing];
    
    self.mainaTableVie.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(onRefresh)];
}

// 下拉刷新
- (void)underRefresh {
    
   
    
    self.p = @"1";
     
    NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
     int t =   [WMUtil  getRandomNumber:101 to:998];
    NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],t];
    
    NSString *typeStr =  [NSString stringWithFormat:@"%@",@"2"];
    
    NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];
    
    NSString *singMD5 = [WMUtil md5HexDigest:singStr];

    self.dataMuArr = [[NSMutableArray alloc]init];
    NSDictionary *parametes = @{
                                       @"p" :self.p,
                                       @"ios" : @"1",
                                       @"ct" :  @"app",
                                       @"ac" : @"mycollect",
                                       @"uid" :uidStr,
                                       @"t" : tStr,
                                       @"type" :typeStr,
                                       @"sign" : singMD5
                                       };
       
       [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
           
           // [SVProgressHUD dismiss];
           [self.mainaTableVie.mj_header endRefreshing];
           [self.mainaTableVie.mj_footer endRefreshing];
           
           
           NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
           
           if ([codeStr intValue] == 1) {
               
               self.dataMuArr = [HWTrdingcollectionModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];
               self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
               self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
               
            
             
               
    
           }
          [self.mainaTableVie reloadData];
           
           
       } failure:^(NSString *error) {
           [self.mainaTableVie.mj_header endRefreshing];
           [self.mainaTableVie.mj_footer endRefreshing];
           
           
       }];
    
    
}

// 上拉加载

- (void)onRefresh {
    
    
   
    int p = [self.p intValue];
                   
    int max_p = [self.max_p intValue];
                   
     if (p > max_p) {
        [MBProgressHUD showError:@"没有更多数据，请待会在上拉加载"];
                       
        return ;
                       
      }
                   
    
    
    NSString *uidStr = [NSString stringWithFormat:@"%@",[HWLocallyStoredData getLoginUserID]];
    
     int t =   [WMUtil  getRandomNumber:101 to:998];
     NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],t];
     
     NSString *typeStr =  [NSString stringWithFormat:@"%@",@"0"];
     
     NSString *singStr = [NSString stringWithFormat:@"%@%@%@",uidStr,tStr,@"fsd213ewdsadqwe2121213edsad"];
     
     NSString *singMD5 = [WMUtil md5HexDigest:singStr];

  
     NSDictionary *parametes = @{
                                         @"p" :self.p,
                                         @"ios" : @"1",
                                         @"ct" :  @"app",
                                         @"ac" : @"mycollect",
                                         @"uid" :uidStr,
                                         @"t" : tStr,
                                         @"type" :typeStr,
                                         @"sign" : singMD5
                                         };
         
        [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
             
             // [SVProgressHUD dismiss];
             [self.mainaTableVie.mj_header endRefreshing];
             [self.mainaTableVie.mj_footer endRefreshing];
             
             
             NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
             
             if ([codeStr intValue] == 1) {
                 
             
               
                
                 NSMutableArray *mutableArr = [HWTrdingcollectionModel  mj_objectArrayWithKeyValuesArray:response[@"data"]];


                 for (int i = 0; i < mutableArr.count; i++) {
                     [self.dataMuArr addObject:mutableArr[i]];

                 }

                 self.p = [NSString stringWithFormat:@"%@",response[@"p"]];
                 self.max_p = [NSString stringWithFormat:@"%@",response[@"max_p"]];
                 [self.mainaTableVie reloadData];
                 
      
             }
          
             
             
         } failure:^(NSString *error) {
             [self.mainaTableVie.mj_header endRefreshing];
             [self.mainaTableVie.mj_footer endRefreshing];
             
             
         }];
    
    
    
    
}






#pragma mark ------------  UITableViewDataSource


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    
    
    return self.dataMuArr.count;
    
    
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *cellID = @"cellID";
     HWJiaoYiShouCangTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[NSBundle mainBundle]loadNibNamed:@"HWJiaoYiShouCangTableViewCell" owner:nil options:nil].firstObject;
        
    }
   // cell.textLabel.text = @"icdihohio'o";
    
    cell.selectionStyle =  UITableViewCellSelectionStyleNone;
    cell.backgroundColor = [UIColor whiteColor];
    HWTrdingcollectionModel *model = self.dataMuArr[indexPath.row];
    cell.dataModel = model;
    [cell.shoucangBut addTarget:self action:@selector(shoucangButTap:) forControlEvents:UIControlEventTouchUpInside];
    cell.shoucangBut.tag = indexPath.row;
    return  cell;
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
   
    
    return  86;
    
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
       HWTrdingcollectionModel *model = self.dataMuArr[indexPath.row];
     
      HWAccountDetailsViewController *accountDetails = [[HWAccountDetailsViewController alloc]init];
       accountDetails.idStr = model.goods_id;
       [self.navigationController pushViewController:accountDetails animated:YES];
    
    
}

- (void)shoucangButTap:(UIButton *) sender {
    
    
    NSLog(@"%ld",sender.tag);
    
    HWTrdingcollectionModel *model = self.dataMuArr[sender.tag];
    
    
    
    NSString *userID = [HWLocallyStoredData getLoginUserID];
    int t =   [WMUtil  getRandomNumber:101 to:998];
       
       
    NSString *tStr = [NSString stringWithFormat:@"%ld%d",[WMUtil getTimeSp],t];
       
   NSString *sign = [NSString stringWithFormat:@"%@%@%@",userID,tStr,@"fsd213ewdsadqwe2121213edsad"];
           NSString *signMD5 = [WMUtil md5HexDigest:sign];
           NSString *typeStr = [NSString stringWithFormat:@"%@",@"2"];
           
           
           
           
    NSDictionary *parametes = @{
                                       @"ct" :  @"app",
                                       @"ac" : @"shoucang_del",
                                       @"ios" : @"1",
                                       @"uid" :userID,
                                       @"t" :tStr,
                                       @"sign": signMD5,
                                       @"type":typeStr,
                                       @"id" : model.id
                                       };
           
           
        [MBProgressHUD showMessage:@"正在取消收藏..."];
           
        [HWWebUtils doGetWithParameters:parametes progress:nil success:^(id response) {
               
             
               NSString *codeStr =  [NSString stringWithFormat:@"%@",response[@"code"]];
               
               
               
               if ([codeStr intValue] == 1) {
                
                 //  self.fenleiMuArr = [response[@"data"] mutableCopy];
                   [MBProgressHUD hideHUD];
                  [self.mainaTableVie.mj_header beginRefreshing];
                   

               } else {
                   
                    [MBProgressHUD  showError:response[@"message"]];
                   
               }
               NSLog(@"%@",response);
               
               
           } failure:^(NSString *error) {
             
               [MBProgressHUD  showError:@"收藏失败"];
               
           }];
       
    
    
    
}


@end
